package controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import models.game.Game;
import models.players.Player;
import models.user.User;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import utils.GameUtils;
import dao.GameDAO;
import dao.UserDAO;

@Controller
public class GameMenuController {
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	UserDAO userDao = (UserDAO) context.getBean("userDao");

	private final static int NUMBER_OF_PLAYERS = 2;
	private final static int NUMBER_OF_ACTIONS = 4;
	private static HashMap<String, Player> players = new HashMap<String, Player>();
	private static HashMap<String, Player> activePlayers = new HashMap<String, Player>();
	private static int turnIndex = 1;
	private static boolean gameStarting = false;
	private static int setPlayers = NUMBER_OF_PLAYERS;
	
	public static HashMap<String, Player> getPlayers() {
		return players;
	}
	public static void decreaseTurnIndex(){
		turnIndex--;
	}
	
	public static boolean getGameStarting() {
		return gameStarting;
	}
	
	public static int getTurnIndex() {
		return turnIndex;
	}

	public static void setTurnIndex(int turnIndex) {
		GameMenuController.turnIndex = turnIndex;
	}

	public static int getNumberOfActions() {
		return NUMBER_OF_ACTIONS;
	}
	
	public static int getNumberOfPlayers() {
		return NUMBER_OF_PLAYERS;
	}

	
	public static HashMap<String, Player> getActivePlayers() {
		return activePlayers;
	}
	
	@RequestMapping("/JoinLobby")
	public synchronized String startGameHandler(HttpServletRequest request) {

		String sessionID = request.getSession().getId();
		String username = request.getSession().getAttribute("username")
				.toString();
		
		// checks if player is already in a game
		if (!activePlayers.containsKey(sessionID)) {
			
			Player player = new Player(username, turnIndex++, sessionID);
			players.put(sessionID, player);
			activePlayers.put(sessionID, player);
			request.getSession().setAttribute("player", player);
			request.getSession().setAttribute("players", players);

			// checks if there is the correct amount of people to start the game
			if (players.size() == NUMBER_OF_PLAYERS) {
				// creates new Game
				Game game = new Game(players);
				for (Player p : players.values()) {
					p.setGame(game);
				}
				// initializes game fields
				GameDAO gameDao = new GameDAO();
				gameDao.createGameObjects(game);
				GameUtils.initGame(game);
				
				//a flag used to display the start button in the lobby
				gameStarting = true;
				
				//returns fields to initial position
				players.clear();
				turnIndex = 1;
				
				return "redirect:/SetSessionAtributes";
			}
		}
		return "Lobby";
	}
	
	//sets attribute with the game in the session of each participant
	@RequestMapping("/SetSessionAtributes")
	public synchronized String setSessionAtributes (HttpServletRequest request) {
		
		request.getSession().setAttribute("game", ((Player)request.getSession().getAttribute("player")).getGame());
		
		if (setPlayers > 1) {
			setPlayers--;
		} else {
			gameStarting = false;
			setPlayers = NUMBER_OF_PLAYERS;
		}
		
		return "redirect:/RedirectPlayers";
	}
	

	@RequestMapping("/Profile")
	public String profileGameHandler(HttpServletRequest request) {
		User user = userDao.getUser((String) request.getSession().getAttribute(
				"username"));
		if (user == null) {
			request.setAttribute("status",
					"Page under construction. We are sorry for the inconvenience!");
			request.setAttribute("email", "");
			request.setAttribute("wins", "");
			request.setAttribute("losses", "");
		} else {
			request.setAttribute("status", "Profile information:");
			request.setAttribute("username", user.getUsername());
			request.setAttribute("email", user.getEmail());
			request.setAttribute("wins", user.getWins());
			request.setAttribute("losses", user.getLosses());
		}
		return "Profile";
	}

	@RequestMapping("/Ranking")
	public String rankUsers(HttpServletRequest request) {
		userDao.rankUsers();
		return "Ranking";
	}
	@RequestMapping("/Rules")
	public String pandemicRules()
	{
		return "Rules";
	}
}
